package com.soulapp.hook.hooker

import android.widget.Toast
import com.highcapable.kavaref.KavaRef.Companion.resolve
import com.highcapable.kavaref.condition.type.Modifiers
import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreator.MemberHookCreator.Result
import com.highcapable.yukihookapi.hook.log.YLog
import com.soulapp.data.ConfigData

// 解除社交能力限制
object LimitSocietyHook : BaseHooker() {
    private const val TAG = "LimitSocietyHook"
    private var hook: Result? = null

    override fun getKey() = ConfigData.limitSociety

    override fun onHook() {
        startHook()
    }

    override fun startHook() {
        YLog.debug("startHook $TAG $hookEnabled")
        if (!hookEnabled) return

        val className = "cn.soulapp.android.component.planet.soulmatch.utils.SocialScoreUtils"
        hook = className.toClass()
            .resolve()
            .optional()
            .method {
                // public static final void b(@Nullable FragmentManager fragmentManager, @Nullable a aVar)
                modifiers(Modifiers.PUBLIC, Modifiers.STATIC, Modifiers.FINAL)
                name("b")
                parameterCount(2)
                returnType = Void::class.javaPrimitiveType
            }.first()
            .apply {
                YLog.debug("hook apply $TAG ${self.name}")
            }
            .hook {
                replaceUnit {
                    val showToast = ConfigData.getBoolean(ConfigData.showToast)
                    if (showToast) {
                        Toast.makeText(appContext, "解除社交能力限制", Toast.LENGTH_SHORT).show()
                    }
                }
                // intercept()
            }
    }

    override fun stopHook() {
        if (hookEnabled) return
        hook?.let {
            it.remove {
                YLog.debug("stopHook $TAG 成功")
            }
        } ?: YLog.debug("stopHook $TAG 失败")

    }

}